<?php

namespace App\Admin\Extensions\RentCloth;

use Encore\Admin\Admin;

class ConfirmOrder
{
    protected $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

    protected function script()
    {
        return <<<SCRIPT
        //隐藏的点击事件
        $(document).on('click', '.btn-success', function() {
            //获取的ID
            var id = $(this).data('id');
            swal({
                title: "确认操作?",
                text: "点击确认则默认收货",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                cancelButtonText: '取消',
                confirmButtonText: '确认'
            }).then((result) =>{
                if(result.value){
                    $.ajax({
                        type: 'post',
                        url: '/admin/rentclothline/confirmOrder',
                        data: {
                            id:id,
                            //post请求token
                            _token:LA.token,
                        },
                        success: function (data) {
                            $.pjax.reload('#pjax-container');
                            if (typeof data === 'object') {
                                if (data.code == 1) {
                                    swal(data.msg, '', 'success');
                                } else {
                                    swal(data.msg, '', 'error');
                                }
                            }
                        }
                    });
                }
            })
        });




SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());

        return "<button type='button' class='btn btn-success' data-id='{$this->id}'>收货</button>";
    }

    public function __toString()
    {
        return $this->render();
    }
}
